package com.lexsoft.kanaiscube.calc;

import java.text.DecimalFormat;

import com.lexsoft.kanaiscube.R;
import com.lexsoft.kanaiscube.util.Tools;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.TextView;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class HydraCalculator extends Fragment implements TextWatcher {

	View view;
	EditText weaponSpeed,speedBonus,handBonus;
	TextView result ;
	double[] hydraLevels;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		view = inflater.inflate(R.layout.fragment_hydra_calculator, container,
				false);
		String[] hl = getResources().getStringArray(R.array.hydar_speed_level_set);
		hydraLevels = new double[hl.length];
		for(int i=0;i<hl.length;i++){
			hydraLevels[i]=Double.parseDouble(hl[i]);
		}
		weaponSpeed = (EditText)view.findViewById(R.id.hydraWeaponSpeed);
		speedBonus = (EditText)view.findViewById(R.id.hydraSpeedBonus);
		handBonus = (EditText)view.findViewById(R.id.hydraHandBonus);
		result = (TextView)view.findViewById(R.id.hydraSpeedResult);
		weaponSpeed.addTextChangedListener(this);
		speedBonus.addTextChangedListener(this);
		handBonus.addTextChangedListener(this);
		calculate();
		String[] dl = getResources().getStringArray(R.array.hydra_speed_damage_set);
		String[] il = getResources().getStringArray(R.array.hydra_speed_increase_set);
		StringBuffer sb = new StringBuffer();
		sb.append("<style type='text/css'>td{border-bottom:1px solid #888;color:#808040}" +
				"th{border-bottom:1px solid #888;color:#c67000;position:relative;z-index:1}" +
				"</style>");
		sb.append("<table cellspacing=2 cellpadding=0 align=center>");
		sb.append("<tr><th nowrap align=center>");
		sb.append(getString(R.string.hydra_calc_hydra_level));
		sb.append("</th><th nowrap align=center>");
		sb.append(getString(R.string.hydra_calc_hydra_speed));
		sb.append("</th><th nowrap align=center>");
		sb.append(getString(R.string.hydra_calc_total_damage));
		sb.append("</th><th nowrap align=center>");
		sb.append(getString(R.string.hydra_calc_damage_increase));
		sb.append("</th></tr>");
		for(int i=0;i<dl.length;i++){
			sb.append("<tr><td nowrap align=center>");
			sb.append(""+(i+1));
			sb.append("</td><td align=center>");
			sb.append(df.format(hydraLevels[i]));
			sb.append("</td><td align=center>");
			sb.append(dl[i]);
			sb.append("</td><td align=center>");
			sb.append(il[i]);
			sb.append("</td></tr>");
		}
		sb.append("</table>");
		WebView webview = (WebView)view.findViewById(R.id.calc_hydra_level_table);
		webview.setBackgroundColor(0);
		webview.loadDataWithBaseURL("about:blank", sb.toString(), "text/html", "utf-8", "");
		return view;
	}

	static DecimalFormat df = new DecimalFormat("0.000");
	static DecimalFormat pdf = new DecimalFormat("0.00%");
	private void calculate(){
		double wSpeed = Tools.getDouble(weaponSpeed.getText().toString(), 0);
		if(wSpeed<=0){
			wSpeed = 1;
			weaponSpeed.setText("1");
		}
		double bSpeed = Tools.getDouble(speedBonus.getText().toString(), 0)/100;
		double hSpeed = Tools.getDouble(handBonus.getText().toString(), 0)/100;
		double hydraRealSpeed = wSpeed * (1+bSpeed)*(1+hSpeed);
		int level = 0;
		double exceed = 0;
		if(hydraRealSpeed<hydraLevels[0]){
			level = 0;
		}else if(hydraRealSpeed>hydraLevels[hydraLevels.length-1]){
			level = hydraLevels.length;
		}else{
			for(int i=0;i<hydraLevels.length;i++){
				if(hydraRealSpeed<hydraLevels[i]){
					level = i;
					exceed = bSpeed - hydraLevels[i-1]/(1+hSpeed)/wSpeed+1;
					break;
				}
			}
		}
		String str = getString(R.string.hydra_calc_hydra_speed)+" "+df.format(hydraRealSpeed)+
				" , "+getString(R.string.hydra_calc_current_hydra_level)+" "+level+
				" , "+getString(R.string.hydra_calc_speed_overload)+" "+ pdf.format(exceed);
		result.setText(str);
	
	}
	@Override
	public void afterTextChanged(Editable arg0) {
		calculate();
	}
	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
	}
	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
	}
}
